package com.lab.tools;

import android.content.Context;
import android.os.PowerManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.cuitrip.service.R;
import com.lab.tools.ui.HttpLog;

import java.util.concurrent.CopyOnWriteArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 桌面悬浮窗主窗口
 * Created by sky on 14-2-12.
 */
public class FloatMainWindowView extends FrameLayout {

    private static final String TAG = "FloatMainWindowView";
    LayoutInflater mInflater;
    private Context mContext;
    private PowerManager.WakeLock mWakeLock;

    @Bind(R.id.main_window)
    View mainWindow;

    @Bind(R.id.close_button)
    TextView closeButton;

    @Bind(R.id.log_list)
    RecyclerView logRecyclerView;

    private static CopyOnWriteArrayList<HttpLog> httpLogs = new CopyOnWriteArrayList<>();

    public FloatMainWindowView(Context context) {
        super(context);
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        setIsScreenAwake(true);

        mInflater.inflate(R.layout.log_float_window, this);
        ButterKnife.bind(this);
    }

    public void setHttpLogs(CopyOnWriteArrayList<HttpLog> httpLogs) {
        this.httpLogs = httpLogs;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        setIsScreenAwake(false);
    }

    @OnClick(R.id.close_button)
    void toggleDetailWindow() {
        mainWindow.setVisibility(GONE);
    }

    public String getTag() {
        return TAG;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN
                && mContext != null && isOutOfBounds(mContext, event)) {
            FloatWindowManager.removeFloatMainWindow(mContext.getApplicationContext());
            return true;
        }
        return false;
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View mainWindowView = this.findViewById(R.id.main_window);
        return (x < -slop) || (y < -slop)
                || (x < (mainWindowView.getLeft() - slop))
                || (x > (mainWindowView.getRight()+slop))
                || (y < (mainWindowView.getTop() - slop))
                || (y > (mainWindowView.getBottom() + slop));
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (mContext != null) {
                FloatWindowManager.removeFloatMainWindow(mContext.getApplicationContext());
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    /**
     * 设置屏幕常亮
     */
    protected void setIsScreenAwake(Boolean isScreenAwake) {
        if (isScreenAwake && mContext != null) {
            mWakeLock = ((PowerManager) getContext().getSystemService(Context.POWER_SERVICE))
                    .newWakeLock(
                            PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,
                            mContext.getPackageName());
            mWakeLock.acquire();
        } else {
            if (mWakeLock != null && mWakeLock.isHeld()) mWakeLock.release();
        }
    }

}
